#
# Copyright (C) Since 2018 ~ 2025, Inc. All Rights Reserved
#
# @Time    : 2025/1/7-上午11:27
# @Author  : Timmy
# @Email   : tangping@cloudstream-tech.com
# @File    : lottery.py
# @Software: lottery
# @Function:
#

import asyncio
import sys

from PyQt5.QtCore import Qt
from qasync import QApplication, QEventLoop, QtCore

from frontend.frontend import web_server
from models.db import init_db, close_db
from terminal.terminal import Terminal
from windows import MainWindow


def main():
    QtCore.QCoreApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
    app = QApplication(sys.argv)

    event_loop = QEventLoop(app)
    event_loop.run_until_complete(init_db())
    asyncio.set_event_loop(event_loop)

    app_close_event = asyncio.Event()
    app.aboutToQuit.connect(app_close_event.set)

    main_window = MainWindow()
    main_window.show()

    event_loop.create_task(main_window.init())

    with event_loop:
        event_loop.run_until_complete(app_close_event.wait())
        event_loop.run_until_complete(close_db())


if __name__ == '__main__':
    # asyncio.run(main())
    main()

